import cv2 as cv
import sys

FPS = 25
INTERVAL = 1000 // FPS

cam = cv.VideoCapture(0)

if not cam.isOpened():
    print('Cannot open cam!')
    sys.exit(1)

while True:
    ret, img = cam.read()
    if not ret:
        print('Cannot get image from cam!')
        break
    cv.imshow('Cam', img)

    if 27 == (0xFF & cv.waitKey(INTERVAL)):
        break

cam.release()
cv.destroyAllWindows()
